package com.totoro.service.impl;

import com.totoro.mapper.ChatMapper;
import com.totoro.pojo.SuMainChat;
import com.totoro.pojo.SuViceChat;
import com.totoro.service.ChatService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author TOTORO
 * @date 2021/8/27 18:24
 */
@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    ChatMapper chatMapper;

    @Override
    public SuMainChat findUserSendPosition(int uid, int suMainChatId) {
        return chatMapper.findUserSendPosition(uid, suMainChatId);
    }

    @Override
    public List<SuMainChat> findUserAllMainChat(int uid, boolean isAll, int sortUid) {
        // 拿到这个用户所有的聊天栏
        List<SuMainChat> suMainChats = chatMapper.findUserAllMainChat(uid, sortUid);
        // 判断是否查询的是所有的，如果是，直接返回上面的数据
        if(isAll){
            return suMainChats;
        }
        // 用户不查询已被屏蔽的：新建集合，进行判断添值
        List<SuMainChat> notDeleteSuMainChats = new ArrayList<>();
        for (SuMainChat suMainChat : suMainChats) {
            if(uid == suMainChat.getFromUid()){
                if(suMainChat.getFromIsDelete() == 0){
                    notDeleteSuMainChats.add(suMainChat);
                }
            } else{
                if(uid == suMainChat.getToUid()){
                    if(suMainChat.getToIsDelete() == 0){
                        notDeleteSuMainChats.add(suMainChat);
                    }
                }
            }
        }
        return notDeleteSuMainChats;
    }

    @SneakyThrows
    @Override
    public Map<String, List<SuViceChat>> findMainChatDownAllChat(int suMainChatId) {
        List<SuViceChat> suViceChats = chatMapper.findMainChatDownAllChat(suMainChatId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 先使用Map将所有分组的时间通过键值对分组
        Map<String, List<SuViceChat>> map = new LinkedHashMap<>();
        for (SuViceChat suViceChat : suViceChats) {
            map.put(suViceChat.getGroupTime(), new ArrayList<SuViceChat>());
        }
        for (SuViceChat suViceChat : suViceChats) {
            map.get(suViceChat.getGroupTime()).add(suViceChat);
        }
        return map;
    }

    @Override
    public SuViceChat findMainChatDownLastChat(int suMainChatId) {
        SuViceChat suViceChat = chatMapper.findMainChatDownLastChat(suMainChatId);
        if(null == suViceChat){
            return new SuViceChat("无");
        } else{
            return suViceChat;
        }
    }

    @Override
    public SuMainChat findAssignChatMain(int fromUid, int toUid) {
        return chatMapper.findAssignChatMain(fromUid, toUid);
    }

    @Override
    public Integer addMainChat(SuMainChat suMainChat) {
        return chatMapper.addMainChat(suMainChat);
    }

    @Override
    public Integer updateMainChatStatus(int sender, int suMainChatId, int status) {
        return chatMapper.updateMainChatStatus(sender, suMainChatId, status);
    }

    @Override
    public Integer updateViceChatReadStatus(int sender, int suMainChatId) {
        return chatMapper.updateViceChatReadStatus(sender, suMainChatId);
    }

    @Override
    public Integer addChatMessage(SuViceChat suViceChat) {
        return chatMapper.addChatMessage(suViceChat);
    }
}
